#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jun  7 15:29:11 2023

@author: jcfq2
"""

import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import numpy as np
import shapely.geometry as sgeom
from cartopy.feature import ShapelyFeature
from scipy.io import readsav
import cartopy.crs as ccrs

import io


# In[0]
# set up values


AddZhang = "True"
AddVogt = "True"

maxint=1.2
minint=0.2

maxint2=1.0
minint2=0.

mincount=0
maxcount=3600*100.#3.*3600#hours - many hours due to bleeding of individual pixels across multiple bins
mindate=10
maxdate=30


minrange=0.7
maxrange=1.9
minrange=0.0003
maxrange=0.0018;0.0001
xxrange=6
startx=1

polelat=75
polelong=180


# In[1]
# load data


results='/Users/jcfq2/OneDrive - Northumbria University - Production Azure AD/results/'
planetary='/Users/jcfq2/OneDrive - Northumbria University - Production Azure AD/python/planetary_values/'


idl_file = readsav(results+'jupimage_phase_total_crazytown.idl')
# idl_file = readsav(results+'jupimage_phase_total_crazytown_avgclicks.idl')
# idl_file = readsav(results+'jupimage_phase_total_crazytown_avgclicks.idl')
a=idl_file['a']
a2=idl_file['a2']
b=idl_file['b']
b2=idl_file['b2']

aa=a
aa2=a2
bb=b
bb2=b2


ab=a/b
ab=np.nan_to_num(ab)

   # In[3] set up vogt


# for cml in range(50,329,10):

               


# In[4] 

totalmap=np.sum(ab,axis=0)
# plt.imshow(totalmap)
# plt.show()

totalmapN=totalmap
totalmapS=totalmap
totalmapN[0:70,:]=0
totalmapS[90:,:]=0

totalmap=totalmap/np.mean(totalmap)
totalmapN=totalmapN/np.mean(totalmapN)
totalmapS=totalmapS/np.mean(totalmapS)

lon = np.linspace(360, 0, 360)
lat = np.linspace(-90, 90, 181)
lon2d, lat2d = np.meshgrid(lon, lat)


crs = ccrs.RotatedPole(globe=ccrs.Globe(flattening=(0.0)))





import sys
import os


from remapped_zhang import get_zhang_mapped

sys.path.insert(0, '../../planetary_values')

from grille_vogt_localtime_dawndusk_all import load_vogt_slices
from grille_vogt_localtime_dawndusk_all import vogt_ofl
from grille_vogt_localtime_dawndusk_all import fit_fieldline_fixed
from grille_vogt_localtime_dawndusk_all import fit_fieldline_scattered


from planet_magfield import Bmag

lt_scaling=[0.8828533281700559, 1.2033086368036778, 1.310723368138646, 1.2050975221749605, 0.8864310989126216]



from shift_magnetic import to_magnetic
from shift_magnetic import from_magnetic
sys.path.insert(0, '../../planetary_values')

# this works great - but I want to use loc values within this function, so am loading and displaying here for assurity
# plot_vogt_slices(3,2,runhere=False)



sector1_lon,sector1_lat,sector1_loc,sector1_dis, sector2_lon,sector2_lat,sector2_loc,sector2_dis, sector3_lon,sector3_lat,sector3_loc,sector3_dis, sector4_lon,sector4_lat,sector4_loc,sector4_dis, sector5_lon,sector5_lat,sector5_loc,sector5_dis = load_vogt_slices()

cml=180
js=80



bbox_props1= dict(boxstyle="round,pad=0.15", fc="aliceblue", ec="gray", lw=1)
lt_text=['08h','10h','12h','14h','16h']



sys.path.insert(0, '../../tss')
from basic.polygonmask import maskeqvalue
from basic.polygonmask import masksubarray
from basic.interpolate_irregular import data_atlatlong

# In[4]
sys.path.insert(0, '../../planetary_values')
   
ofl_lat,ofl_lon=vogt_ofl()
    
subsolarlat=0
subsolarlongitude=180#240.3#+180


s_ssl=str(int(subsolarlongitude/10+1)*10)

# print(s_ssl)

sys.path.insert(0, '../../planetary_values')

v = open(os.path.join(sys.path[0],'fieldline_tracing/fieldline_tracing_mapping_contours_kk2009ext_jrm09int_north_sslong'+s_ssl+'.txt'))
north_plotting = np.zeros([28,36,4])

header = v.readline()

for z in range(28):
    for y in range(36):
        line = v.readline()
        north_plotting[z,y,0]=line[0:12]
        north_plotting[z,y,1]=line[14:20]
        null1=line[21:29]
        if any(chr.isdigit() for chr in null1):
            north_plotting[z,y,2]=null1
        else:
            north_plotting[z,y,2]=np.nan
        null2=line[30:38]
        if any(chr.isdigit() for chr in null2):
            north_plotting[z,y,3]=null2
        else:
            north_plotting[z,y,3]=np.nan
                              
    


# In[4]


render='True'



############  render the panels


if render == 'True':
    for i in range(startx,xxrange):

        
        cml=360-(90+i*20+10)   
        
        
        subsolarlat=0
        subsolarlongitude=cml#240.3#+180
        
        mapfull=ab

        mapmap=np.sum(mapfull[75+i*30:75+(i+1)*30,:,:],axis=0)
        mapmapT=np.sum(mapfull[75+1*30:75+(5+1)*30,:,:],axis=0)
        mean_mapmapT=np.mean(np.nonzero(mapmapT))
         

        ax0=1120.572303190426
        ax1=-43.641738021841704#g.parameters[0]

        B1=Bmag(lon2d, lat2d,radial=False)
        
        scaleint=B1*ax1+ax0
        scaleint=scaleint/5 #because these slices are a fifth the size of the full total

        mapmap_scaled=mapmap/np.reshape(scaleint,mapmap.shape)

        ynum=3
     
        
        mapmap_scaled2=mapmap_scaled/lt_scaling[i-1]
           

        ypos=1
        ypos=(ypos-1)*5

        
        scaling=0.5



        fig_temp = plt.figure(figsize=(10*scaling,10*scaling),dpi=100,facecolor='w')
        
        io_buf = io.BytesIO()
        
        ax_temp = plt.subplot(1,1,1,projection=ccrs.Orthographic(360-polelong, polelat))
        cc1=ax_temp.contourf(lon2d, lat2d, mapmap_scaled2,transform=ccrs.PlateCarree(),levels=256,cmap='afmhot',vmax=maxint,vmin=minint)
        ax_temp.set_extent([150, 210, 52, 90], crs=ccrs.PlateCarree())

        window_extent = ax_temp.get_window_extent()
        fig_temp.savefig(io_buf, format='raw', bbox_inches='tight', pad_inches=0)
        io_buf.seek(0)
        
        img_arr = np.reshape(np.frombuffer(io_buf.getvalue(), dtype=np.uint8),
                          newshape=(int(window_extent.height), int(window_extent.width), -1))



     
        if i == 1:
            int_1 = img_arr

        if i == 2:
            int_2 = img_arr
 
    
        if i == 3:
            int_3 = img_arr

        if i == 4:
            int_4 = img_arr
 
    
        if i == 5:
            int_5 = img_arr



if render == 'False':


       fig_cc=plt.figure()
       cc1=plt.imshow(totalmap)


# In[5]
brightness=np.zeros([xxrange,36])
brightness_scaled=np.zeros([xxrange,36])
brightness_scaled2=np.zeros([xxrange,36])




#############   plot the plot #################




fig = plt.figure()


fig.set_figwidth(10)
fig.set_figheight(10/5*3)
fig.set_dpi(300)
fig.subplots_adjust(wspace=0, hspace=0)




for i in range(startx,xxrange):

    if render == 'True':
    
        if i == 1:
            int_x = int_1
            
        if i == 2:
            int_x = int_2
            
        if i == 3:
            int_x = int_3
     
        if i == 4:
            int_x = int_4
     
        if i == 5:
            int_x = int_5



    cml=360-(90+i*20+10)   
    
    subsolarlat=0
    subsolarlongitude=cml#240.3#+180
    
    
    s_ssl=str(int(subsolarlongitude/10+1)*10)
    
    

    mapfull=ab

    mapmap=np.sum(mapfull[75+i*30:75+(i+1)*30,:,:],axis=0)
    mapmapT=np.sum(mapfull[75+1*30:75+(5+1)*30,:,:],axis=0)
    mean_mapmapT=np.mean(np.nonzero(mapmapT))
    

    ax0=1120.572303190426
    ax1=-43.641738021841704#g.parameters[0]

    B1=Bmag(lon2d, lat2d,radial=False)
    
    scaleint=B1*ax1+ax0
    scaleint=scaleint/5 #because these slices are a fifth the size of the full total

    mapmap_scaled=mapmap/np.reshape(scaleint,mapmap.shape)

    ynum=3
 
    
    mapmap_scaled2=mapmap_scaled/lt_scaling[i-1]
       
    current_z_ofl_lat_scl_p,current_z_ofl_lon_scl_p,current_z_ocr_lat_scl_p,current_z_ocr_lon_scl_p,current_z_dcl_lat_scl_p,current_z_dcl_lon_scl_p = get_zhang_mapped(cml=subsolarlongitude)

       


    ypos=1
    ypos=(ypos-1)*5

    if i == 1:
        
        
        a3x1_base = plt.subplot(ynum, 5, ypos+(6-i),projection=ccrs.Orthographic(360-polelong, polelat))
        gl = a3x1_base.gridlines(crs=ccrs.PlateCarree(), linewidth=1, color='grey', alpha=0.0, linestyle='dotted', draw_labels=False)
 
        # a31i=a3x1.contourf(lon2d, lat2d, mapmap_scaled2,transform=ccrs.PlateCarree(),levels=256,cmap='afmhot',vmax=maxint,vmin=minint) 
        a3x1_chartBox = a3x1_base.get_position()
        a3x1_img = fig.add_axes([a3x1_chartBox.x0, a3x1_chartBox.y0, 
                     a3x1_chartBox.width, 
                     a3x1_chartBox.height], transform=a3x1_base.transAxes, frameon=False) 
        a3x1_img.set_axis_off()
        a3x1_base.set_axis_off()

        fig.set_facecolor("white")
        
        if render == 'True':

            a3x1_img.imshow(int_x)

        a3x1 = fig.add_axes([a3x1_chartBox.x0, a3x1_chartBox.y0, 
                          a3x1_chartBox.width, 
                          a3x1_chartBox.height], transform=a3x1_base.transAxes,projection=ccrs.Orthographic(360-polelong, polelat), frameon=False) 

        
 
        a3x1.set_extent([150, 210, 52, 90], crs=ccrs.PlateCarree())
        if AddVogt == "True":
            a3x1.plot(north_plotting[3,:,3],north_plotting[3,:,2],transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5)
            a3x1.plot(north_plotting[13,:,3],north_plotting[13,:,2],transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5,linestyle='--')
            a3x1.plot(north_plotting[23,:,3],north_plotting[23,:,2],transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5,linestyle='--')

        if AddZhang == "True":

            a3x1.plot(current_z_ofl_lon_scl_p,current_z_ofl_lat_scl_p,transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5,c='lime')
            a3x1.plot(current_z_ocr_lon_scl_p,current_z_ocr_lat_scl_p,transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5,c='blue')
            a3x1.plot(current_z_dcl_lon_scl_p,current_z_dcl_lat_scl_p,transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5,c='grey')
        a3x1.text(360-102, 77, lt_text[4],transform=ccrs.PlateCarree() , ha="center", va="center",c='k',zorder=4,fontsize='small',bbox=bbox_props1)

    if i == 2:

        
        a3x2_base = plt.subplot(ynum, 5, ypos+(6-i),projection=ccrs.Orthographic(360-polelong, polelat))
        gl = a3x2_base.gridlines(crs=ccrs.PlateCarree(), linewidth=1, color='grey', alpha=0.0, linestyle='dotted', draw_labels=False)
 
        # a31i=a3x2.contourf(lon2d, lat2d, mapmap_scaled2,transform=ccrs.PlateCarree(),levels=256,cmap='afmhot',vmax=maxint,vmin=minint) 
        a3x2_chartBox = a3x2_base.get_position()
        a3x2_img = fig.add_axes([a3x2_chartBox.x0, a3x2_chartBox.y0, 
                     a3x2_chartBox.width, 
                     a3x2_chartBox.height], transform=a3x2_base.transAxes, frameon=False) 
        a3x2_img.set_axis_off()
        a3x2_base.set_axis_off()
       
        if render == 'True':

            a3x2_img.imshow(int_x)

        a3x2 = fig.add_axes([a3x2_chartBox.x0, a3x2_chartBox.y0, 
                          a3x2_chartBox.width, 
                          a3x2_chartBox.height], transform=a3x2_base.transAxes,projection=ccrs.Orthographic(360-polelong, polelat), frameon=False) 


        a3x2.set_extent([150, 210, 52, 90], crs=ccrs.PlateCarree())
        if AddVogt == "True":
            a3x2.plot(north_plotting[3,:,3],north_plotting[3,:,2],transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5)
            a3x2.plot(north_plotting[13,:,3],north_plotting[13,:,2],transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5,linestyle='--')
            a3x2.plot(north_plotting[23,:,3],north_plotting[23,:,2],transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5,linestyle='--')

        if AddZhang == "True":
            a3x2.plot(current_z_ofl_lon_scl_p,current_z_ofl_lat_scl_p,transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5,c='lime')
            a3x2.plot(current_z_ocr_lon_scl_p,current_z_ocr_lat_scl_p,transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5,c='blue')
            a3x2.plot(current_z_dcl_lon_scl_p,current_z_dcl_lat_scl_p,transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5,c='grey')
        a3x2.text(360-102, 77, lt_text[3],transform=ccrs.PlateCarree() , ha="center", va="center",c='k',zorder=4,fontsize='small',bbox=bbox_props1)

    if i == 3:

        a3x3_base = plt.subplot(ynum, 5, ypos+(6-i),projection=ccrs.Orthographic(360-polelong, polelat))
        gl = a3x3_base.gridlines(crs=ccrs.PlateCarree(), linewidth=1, color='grey', alpha=0.0, linestyle='dotted', draw_labels=False)
 
        # a31i=a3x3.contourf(lon2d, lat2d, mapmap_scaled2,transform=ccrs.PlateCarree(),levels=256,cmap='afmhot',vmax=maxint,vmin=minint) 
        a3x3_chartBox = a3x3_base.get_position()
        a3x3_img = fig.add_axes([a3x3_chartBox.x0, a3x3_chartBox.y0, 
                     a3x3_chartBox.width, 
                     a3x3_chartBox.height], transform=a3x3_base.transAxes, frameon=False) 
        a3x3_img.set_axis_off()
        a3x3_base.set_axis_off()
        fig.set_facecolor("white")
        
        if render == 'True':

            a3x3_img.imshow(int_x)

        a3x3 = fig.add_axes([a3x3_chartBox.x0, a3x3_chartBox.y0, 
                          a3x3_chartBox.width, 
                          a3x3_chartBox.height], transform=a3x3_base.transAxes,projection=ccrs.Orthographic(360-polelong, polelat), frameon=False) 


        a3x3.set_extent([150, 210, 52, 90], crs=ccrs.PlateCarree())
        if AddVogt == "True":
            a31i=a3x3.plot(north_plotting[3,:,3],north_plotting[3,:,2],transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5)
            a3x3.plot(north_plotting[13,:,3],north_plotting[13,:,2],transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5,linestyle='--')
            a3x3.plot(north_plotting[23,:,3],north_plotting[23,:,2],transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5,linestyle='--')
        if AddZhang == "True":

            a3x3.plot(current_z_ofl_lon_scl_p,current_z_ofl_lat_scl_p,transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5,c='lime')
            a3x3.plot(current_z_ocr_lon_scl_p,current_z_ocr_lat_scl_p,transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5,c='blue')
            a3x3.plot(current_z_dcl_lon_scl_p,current_z_dcl_lat_scl_p,transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5,c='grey')
        a3x3.text(360-102, 77, lt_text[2],transform=ccrs.PlateCarree() , ha="center", va="center",c='k',zorder=4,fontsize='small',bbox=bbox_props1)

    if i == 4:

        a3x4_base = plt.subplot(ynum, 5, ypos+(6-i),projection=ccrs.Orthographic(360-polelong, polelat))
        gl = a3x4_base.gridlines(crs=ccrs.PlateCarree(), linewidth=1, color='grey', alpha=0.0, linestyle='dotted', draw_labels=False)
 
        # a31i=a3x4.contourf(lon2d, lat2d, mapmap_scaled2,transform=ccrs.PlateCarree(),levels=256,cmap='afmhot',vmax=maxint,vmin=minint) 
        a3x4_chartBox = a3x4_base.get_position()
        a3x4_img = fig.add_axes([a3x4_chartBox.x0, a3x4_chartBox.y0, 
                     a3x4_chartBox.width, 
                     a3x4_chartBox.height], transform=a3x4_base.transAxes, frameon=False) 
        a3x4_img.set_axis_off()
        a3x4_base.set_axis_off()
        fig.set_facecolor("white")
        
        if render == 'True':

            a3x4_img.imshow(int_x)

        a3x4 = fig.add_axes([a3x4_chartBox.x0, a3x4_chartBox.y0, 
                          a3x4_chartBox.width, 
                          a3x4_chartBox.height], transform=a3x4_base.transAxes,projection=ccrs.Orthographic(360-polelong, polelat), frameon=False) 


        a3x4.set_extent([150, 210, 52, 90], crs=ccrs.PlateCarree())
        if AddVogt == "True":
            a3x4.plot(north_plotting[3,:,3],north_plotting[3,:,2],transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5)
            a3x4.plot(north_plotting[13,:,3],north_plotting[13,:,2],transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5,linestyle='--')
            a3x4.plot(north_plotting[23,:,3],north_plotting[23,:,2],transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5,linestyle='--')
        if AddZhang == "True":
            a3x4.plot(current_z_ofl_lon_scl_p,current_z_ofl_lat_scl_p,transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5,c='lime')
            a3x4.plot(current_z_ocr_lon_scl_p,current_z_ocr_lat_scl_p,transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5,c='blue')
            a3x4.plot(current_z_dcl_lon_scl_p,current_z_dcl_lat_scl_p,transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5,c='grey')
        a3x4.text(360-102, 77, lt_text[1],transform=ccrs.PlateCarree() , ha="center", va="center",c='k',zorder=4,fontsize='small',bbox=bbox_props1)
    
    if i == 5:

        a3x5_base = plt.subplot(ynum, 5, ypos+(6-i),projection=ccrs.Orthographic(360-polelong, polelat), alpha=0.0 )
        gl = a3x5_base.gridlines(crs=ccrs.PlateCarree(), linewidth=1, color='grey', alpha=0.0, linestyle='dotted', draw_labels=False)
 
        # a31i=a3x5.contourf(lon2d, lat2d, mapmap_scaled2,transform=ccrs.PlateCarree(),levels=256,cmap='afmhot',vmax=maxint,vmin=minint) 
        a3x5_chartBox = a3x5_base.get_position()
        a3x5_img = fig.add_axes([a3x5_chartBox.x0, a3x5_chartBox.y0, 
                     a3x5_chartBox.width, 
                     a3x5_chartBox.height], transform=a3x5_base.transAxes, frameon=False) 
        a3x5_img.set_axis_off()
        a3x5_base.set_axis_off()
        fig.set_facecolor("white")
        
        if render == 'True':

            a3x5_img.imshow(int_x)

        a3x5 = fig.add_axes([a3x5_chartBox.x0, a3x5_chartBox.y0, 
                          a3x5_chartBox.width, 
                          a3x5_chartBox.height], transform=a3x5_base.transAxes,projection=ccrs.Orthographic(360-polelong, polelat), frameon=False) 


        a3x5.set_extent([150, 210, 52, 90], crs=ccrs.PlateCarree())
        if AddVogt == "True":
            a3x5.plot(north_plotting[3,:,3],north_plotting[3,:,2],transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5)
            a3x5.plot(north_plotting[13,:,3],north_plotting[13,:,2],transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5,linestyle='--')
            a3x5.plot(north_plotting[23,:,3],north_plotting[23,:,2],transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5,linestyle='--')
        if AddZhang == "True":
            a3x5.plot(current_z_ofl_lon_scl_p,current_z_ofl_lat_scl_p,transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5,c='lime')
            a3x5.plot(current_z_ocr_lon_scl_p,current_z_ocr_lat_scl_p,transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5,c='blue')
            a3x5.plot(current_z_dcl_lon_scl_p,current_z_dcl_lat_scl_p,transform=ccrs.PlateCarree(),linewidth=2,alpha=0.5,c='grey')
   
        a3x5.text(360-102, 77, lt_text[0],transform=ccrs.PlateCarree() , ha="center", va="center",c='k',zorder=4,fontsize='small',bbox=bbox_props1)
  

     


    ypos=2
    ypos=(ypos-1)*5

    if i == 1:

        a4x1 = plt.subplot(ynum, 5, ypos+(6-i),projection=ccrs.Orthographic(180, 80))
        gl = a4x1.gridlines(crs=ccrs.PlateCarree(), linewidth=1, color='black', alpha=0.5, linestyle='dotted', draw_labels=False)
        a4x1.set_extent([150, 210, 52, 90], crs=ccrs.PlateCarree())
        a4x1.scatter(sector1_lon,sector1_lat,transform=ccrs.PlateCarree(),c=sector1_loc,vmin=0,vmax=24,cmap='hsv',marker='.')
   
        a4x1.text(360-102, 77, lt_text[4],transform=ccrs.PlateCarree() , ha="center", va="center",c='k',zorder=4,fontsize='small',bbox=bbox_props1)
 
   
    if i == 2:

        a4x2 = plt.subplot(ynum, 5, ypos+(6-i),projection=ccrs.Orthographic(180, 80))
        gl = a4x2.gridlines(crs=ccrs.PlateCarree(), linewidth=1, color='black', alpha=0.5, linestyle='dotted', draw_labels=False)
        a4x2.set_extent([150, 210, 52, 90], crs=ccrs.PlateCarree())
        a4x2.scatter(sector2_lon,sector2_lat,transform=ccrs.PlateCarree(),c=sector2_loc,vmin=0,vmax=24,cmap='hsv',marker='.')

        a4x2.text(360-102, 77, lt_text[3],transform=ccrs.PlateCarree() , ha="center", va="center",c='k',zorder=4,fontsize='small',bbox=bbox_props1)

    if i == 3:

        a4x3 = plt.subplot(ynum, 5, ypos+(6-i),projection=ccrs.Orthographic(360-polelong, polelat))
        gl = a4x3.gridlines(crs=ccrs.PlateCarree(), linewidth=1, color='black', alpha=0.5, linestyle='dotted', draw_labels=False)      
        a4x3.set_extent([150, 210, 52, 90], crs=ccrs.PlateCarree())
        a4x3.scatter(sector3_lon,sector3_lat,transform=ccrs.PlateCarree(),c=sector3_loc,vmin=0,vmax=24,cmap='hsv',marker='.')
        a4x3.text(360-102, 77, lt_text[2],transform=ccrs.PlateCarree() , ha="center", va="center",c='k',zorder=4,fontsize='small',bbox=bbox_props1)


    if i == 4:

        a4x4 = plt.subplot(ynum, 5, ypos+(6-i),projection=ccrs.Orthographic(180, 80))
        gl = a4x4.gridlines(crs=ccrs.PlateCarree(), linewidth=1, color='black', alpha=0.5, linestyle='dotted', draw_labels=False)
        a4x4.set_extent([150, 210, 52, 90], crs=ccrs.PlateCarree())
        a4x4.scatter(sector4_lon,sector4_lat,transform=ccrs.PlateCarree(),c=sector4_loc,vmin=0,vmax=24,cmap='hsv',marker='.')
        a4x4.text(360-102, 77, lt_text[1],transform=ccrs.PlateCarree() , ha="center", va="center",c='k',zorder=4,fontsize='small',bbox=bbox_props1)

    if i == 5:

        subsolarlongitude=240
        a4x5 = plt.subplot(ynum, 5, ypos+(6-i),projection=ccrs.Orthographic(360-polelong, polelat))
        gl = a4x5.gridlines(crs=ccrs.PlateCarree(), linewidth=1, color='black', alpha=0.5, linestyle='dotted', draw_labels=False)    
        a4x5.set_extent([150, 210, 52, 90], crs=ccrs.PlateCarree())
        cc2=a4x5.scatter(sector5_lon,sector5_lat,transform=ccrs.PlateCarree(),c=sector5_loc,vmin=0,vmax=24,cmap='hsv',marker='.')
        a4x5.text(360-102, 77, lt_text[0],transform=ccrs.PlateCarree() , ha="center", va="center",c='k',zorder=4,fontsize='small',bbox=bbox_props1)

        
 



a5x2= plt.subplot(5, 2, 9)
a5x2.axis('off')

cbar=fig.colorbar(cc1,ax=a5x2,location='top', ticks=[0.0,0.2,0.4,0.6,0.8,1.0,1.2],aspect=16)
cbar.set_label('Emission intensity')

a5x4= plt.subplot(5,2,10)
a5x4.axis('off')

cbar2=fig.colorbar(cc2,ax=a5x4,location='top', ticks=[0,6,12,18,24],aspect=16)
cbar2.ax.set_xticklabels(['0h','06h','12h','18h','24h'])
cbar2.set_label('Local time')

        
fig.savefig('asd_scaling.pdf', dpi=300, bbox_inches='tight', facecolor='white') 
            
